package com.suanfa;


import java.util.Arrays;

/**
 * 二分查找
 * @Author weijingchen
 * @Date 2025/5/27 22:02
 * @Version 1.0.0
 */
public class Test05 {
    public static void main(String[] args) {
        int [] arr = {1,4,3,7,5,80
                ,7,6,9,10};
        int num = 3;
        System.out.println(binarySearch(arr, num));
    }

    private static boolean binarySearch(int[] arr, int num) {
            int left = 0;
            int right = arr.length - 1;
            //先给arr进行排序
            for (int i = 0; i < arr.length; i++) {
                for (int j = i + 1; j < arr.length; j++) {
                    if (arr[i] > arr[j]) {
                        int temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                    }
                }
            }

        System.out.println(Arrays.toString(arr));
            while (left <= right) {
                int mid = (left + right) / 2;
                if (arr[mid] == num) {
                    return true;
                } else if (arr[mid] > num) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            }
            return false;
    }
}
